﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Facade;
using IronOne.SalesOne.BusinessObjects;
using IronOne.SalesOne.SalesOneWeb;
using System.Text;
using System.Text.RegularExpressions;

namespace IronOne.SalesOne.SalesOneWeb.Collection
{
    public partial class VerifyCollectionTransactionDetails : SOSecurityPage,IDataPage
    {
        string SOCode;
        string CSSNo;
        string Status;
        bool HQFMgr = false;
        bool Cachier = false;

        StructureFacade facade;
        FinanceFacade finFac = new FinanceFacade();
        int userType;
        private static int pageNo = 1;
        double cashAmount = 0;
        double chqAmount = 0;
        Dictionary<long, string> cheqDetails = new Dictionary<long, string>();
        StringCollection strCollection;

        protected void Page_Load(object sender, EventArgs e)
        {
            //string postCause = this.Page.Request.Params.Get("__EVENTTARGET");

            strCollection = new StringCollection();
            strCollection = ValidateUserCredentials(Request.Url.LocalPath);

            //btnCancelTXN.Visible = true;
            if (!string.IsNullOrEmpty(Request.QueryString["Status"]) && Request.QueryString["Status"] == "Completed")
            {

                btnVerify.Visible = false;
                btnCancelTXN.Visible = false;
            }
            
            
            facade = new StructureFacade();
            if (Request.QueryString["CSSNo"] != null)
            {
                CSSNo = Request.QueryString["CSSNo"].ToString();
                Session["CSSNo"] = CSSNo;
            }
            else
            {
                CSSNo = Session["CSSNo"].ToString();
                //CSSNo = "1";
            }
            if (Request.QueryString["SOCode"] != null)
            {
                SOCode = Request.QueryString["SOCode"].ToString();
                Session["SOCode"] = SOCode;
            }
            else
            {
                SOCode = Session["SOCode"].ToString();
                //SOCode = "SO1";
            }
            if (Request.QueryString["Status"] != null)
            {
                Status = Request.QueryString["Status"].ToString();
                Session["Status"] = Status;
            }
            else
            {
                Status = Session["Status"].ToString();
                //Status = "SUBMITTED";
            }
            

            //Check Permissions
            if (strCollection.Contains("d") && strCollection.Contains("u"))//Update or Delete permission :- Cancel TXN and Verify
            {
                HQFMgr = true;
                Cachier = false;
            }
            else if (strCollection.Contains("u"))//Update only :- Only Verify
            {
                Cachier = true;
                HQFMgr = false;
            }
            else if (strCollection.Contains("v"))//View only
            {
                Cachier = false;
                HQFMgr = false;
            }

            if (!Page.IsPostBack)
            {
                //strCollection = new StringCollection();
                //strCollection = ValidateUserCredentials(Request.Url.LocalPath);

                User user = facade.GetUser(User.Identity.Name);
                UserInformation info = facade.GetUserInformation(User.Identity.Name);
                userType = info.UserType;
                Session["CheqDetailsDic"] = cheqDetails;
                ViewState["UserType"] = userType;
                showGridData(); 

            }
            if (ViewState["UserType"] != null && Convert.ToInt32(ViewState["UserType"]) != 1)
            {
                //btnCancelTXN.Attributes["Style"] = "Display:block";
                btnTXN.Attributes["Style"] = "Display:none";
            }

            if (HQFMgr)
            {
                btnTXN.Attributes["Style"] = "display:block";
            }
            if (Convert.ToInt32(ViewState["UserType"]) == 1) 
            {
                btnTXN.Attributes["Style"] = "display:block";
            }
            else
            {
                btnTXN.Attributes["Style"] = "Display:none";
            }


            
        }

        public string GetSOCode()
        {
            return SOCode;
        }

        private void showGridData()
        {
            List<CollectionTransaction> collectioCashList = new List<CollectionTransaction>();
            collectioCashList = finFac.GetCSSDetailsByCSSNo(CSSNo, "CSH", SOCode, Status, HQFMgr);
            bool isHeaderFilled = false;
            lblSocode.Text = SOCode;
            lblCssno.Text = CSSNo;

            if (collectioCashList.Count == 0)
            {
                //TblCash.Attributes["style"] = "display:none";
                lblCashAmt.Text = "0.00";
            }
            else
            {
                lblLastSyncDate.Text = collectioCashList[0].LastSyncDate.ToShortDateString().ToString();
                lblLastSyncTime.Text = collectioCashList[0].LastSyncDate.TimeOfDay.ToString();
                isHeaderFilled = true;
            }

            List<CollectionTransaction> collectioChqList = new List<CollectionTransaction>();
            collectioChqList = finFac.GetCSSDetailsByCSSNo(CSSNo, "CHQ", SOCode, Status, HQFMgr);
            if (collectioChqList.Count == 0)
            {
                //TblCheque.Attributes["style"] = "display:none";
                lblChqAmt.Text = "0.00";
            }
            else
            {
                
                if (!isHeaderFilled)
                {
                    lblLastSyncDate.Text = collectioChqList[0].LastSyncDate.ToShortDateString().ToString();
                    lblLastSyncTime.Text = collectioChqList[0].LastSyncDate.TimeOfDay.ToString();
                    isHeaderFilled = true;
                }
            }
            GridVCash.DataSource = collectioCashList;
            GridVCash.DataBind();

            GridVCheque.DataSource = collectioChqList;
            GridVCheque.DataBind();

            

            Session["CheqDetailsDic"] = cheqDetails;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (Session["CSSNo"] != null)
            {
                Session.Remove("CSSNo");
            } 

            Response.Redirect("~/Collection/SearchVerifyCollectionTransaction.aspx?load=true", false);
        }

        protected void btnCancelTXN_Click(object sender, EventArgs e)
        {
            Dictionary<long, string> cheqDetails = new Dictionary<long, string>();
            cheqDetails = (Dictionary<long, string>)Session["CheqDetailsDic"];
            string validCash = ValidateGridData(GridVCash);
            string validCheq = ValidateGridData(GridVCheque);
            int chekedCount = 0;

            if (validCash == "VALID" && validCheq == "VALID")
            {
                int rowCount = 0;
                foreach (GridViewRow gvr in GridVCash.Rows)
                {
                    if (gvr.RowType == DataControlRowType.DataRow)
                    {
                        if (((CheckBox)(gvr.Cells[0].Controls[1])).Checked)
                        {
                            if (gvr.Cells[6].Text.Trim().Equals("SUBMITTED", StringComparison.OrdinalIgnoreCase))
                            {
                                chekedCount++;

                                //CancelTXN
                                int res = finFac.CancelCollection(SOCode,
                                                            Convert.ToInt64(((LinkButton)gvr.Cells[1].Controls[1]).Text.Trim()),
                                                            "CancelledVerified",User.Identity.Name);

                                if (res == 1)
                                {
                                    rowCount++;
                                }
                            }
                        }

                    }
                }

                foreach (GridViewRow gvr in GridVCheque.Rows)
                {
                    if (gvr.RowType == DataControlRowType.DataRow)
                    {

                        if (((CheckBox)(gvr.Cells[0].Controls[1])).Checked)
                        {
                            if (gvr.Cells[7].Text.Trim().Equals("SUBMITTED", StringComparison.OrdinalIgnoreCase))
                            {
                                chekedCount++;

                                //CancelTXN
                                int res = finFac.CancelCollection(SOCode,
                                                            Convert.ToInt64(((LinkButton)gvr.Cells[1].Controls[1]).Text.Trim()),
                                                            "CancelledVerified", User.Identity.Name);

                                if (res == 1)
                                {
                                    rowCount++;
                                }
                            }
                        }
                    }
                }

                if (chekedCount > 0)
                {
                    if (rowCount > 0)
                    {
                        MassageSummery("IIS408");
                        showGridData();
                    }
                }
                else
                {
                    MassageSummery("IIS536");
                }

                //showGridData();
            }

            else if (validCash == "ZEROLOAN" || validCheq == "ZEROLOAN")
            {
                MassageSummery("IIS534");
            }

            else if (validCash == "STRLOAN" || validCheq == "STRLOAN")
            {
                MassageSummery("IIS535");
            }

            else if (validCash == "INVALID" || validCheq == "INVALID")
            {
                MassageSummery("IIS536");
            }


        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        protected void GridVCash_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lnkRecNo = (LinkButton)(e.Row.Cells[1].Controls[1]);
                TextBox txtContract = (TextBox)(e.Row.Cells[2].Controls[1]);
                TextBox txtLoanInt = (TextBox)(e.Row.Cells[3].Controls[1]);
                HyperLink hypDetails = (HyperLink)(e.Row.Cells[10].Controls[1]);
                DropDownList ddlBankStatus = (DropDownList)(e.Row.Cells[9].Controls[1]);

                if (!(((CollectionTransaction)(e.Row.DataItem)).Status.ToLower().Equals("cancelled")))
                {

                    cashAmount = cashAmount + (double)((CollectionTransaction)(e.Row.DataItem)).CollectionAmount;
                }
                //jayanga
                //Loan Textbox
                if (e.Row.Cells[5].Text.Equals("LOAN",StringComparison.OrdinalIgnoreCase))
                {
                    if (HQFMgr || Cachier)
                    {
                        txtLoanInt.ReadOnly = false;
                    }
                    else
                    {
                        txtLoanInt.ReadOnly = true;
                    }
                    txtLoanInt.Text = ((CollectionTransaction)(e.Row.DataItem)).LoanInterest.ToString();
                }
                else
                {
                    txtLoanInt.Text = ((CollectionTransaction)(e.Row.DataItem)).LoanInterest.ToString();
                    txtLoanInt.ReadOnly = true;
                }

                //Contract Or Client textbox
                if (e.Row.Cells[5].Text.Equals("RENEWAL", StringComparison.OrdinalIgnoreCase) || e.Row.Cells[5].Text.Equals("LOAN", StringComparison.OrdinalIgnoreCase))
                {
                    txtContract.Text = ((CollectionTransaction)(e.Row.DataItem)).ContractNo;
                    txtContract.ReadOnly = true;
                }
                else if (e.Row.Cells[5].Text.Equals("DEPOSIT", StringComparison.OrdinalIgnoreCase))
                {
                   // txtContract.Text = ((CollectionTransaction)(e.Row.DataItem)).ContractNo;
                    txtContract.Text = ((CollectionTransaction)(e.Row.DataItem)).ClientID;
                    if (HQFMgr || Cachier)
                    {
                        txtContract.ReadOnly = false;
                    }
                    else
                    {
                        txtContract.ReadOnly = true;
                    }
                }

                lblCashAmt.Text = cashAmount.ToString();
                //

                //Set Bank Status
                if (((CollectionTransaction)(e.Row.DataItem)).BankedStatus == "No")
                {
                    ddlBankStatus.SelectedIndex = 1;
                }
                else
                {
                    ddlBankStatus.SelectedIndex = 0;
                }

                //Sete Navigate URL and Texts to HyperLinks
                lnkRecNo.Text = ((CollectionTransaction)(e.Row.DataItem)).ReceiptNo.ToString();
                //set Javascript String
                //lnkRecNo.Attributes.Add("OnClientClick", SetJavascriptString(lnkRecNo.Text));

                hypDetails.NavigateUrl = "TransactionRemarkDetails.aspx?RecNo=" + ((CollectionTransaction)(e.Row.DataItem)).ReceiptNo.ToString() + "&ContNo=" + txtContract.Text + "&SOCode=" + SOCode;

                if (e.Row.Cells[6].Text.Equals("VERIFIED", StringComparison.OrdinalIgnoreCase))
                {
                    if (ViewState["UserType"] != null && Convert.ToInt32(ViewState["UserType"]) != 1)
                    {
                        ((CheckBox)(e.Row.Cells[0].Controls[1])).Enabled = false;
                    }

                    if (HQFMgr)
                    {
                        ((CheckBox)(e.Row.Cells[0].Controls[1])).Enabled = true;
                    }

                    txtContract.ReadOnly = true;
                    txtLoanInt.ReadOnly = true;
                    ddlBankStatus.Enabled = false;
                }

            }
        }

        protected void GridVCheque_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lnkRecNo = (LinkButton)(e.Row.Cells[1].Controls[1]);
                
                TextBox txtContract = (TextBox)(e.Row.Cells[2].Controls[1]);
                
                TextBox txtLoanInt = (TextBox)(e.Row.Cells[3].Controls[1]);
                HyperLink hypDetails = (HyperLink)(e.Row.Cells[11].Controls[1]);
                DropDownList ddlBankStatus = (DropDownList)(e.Row.Cells[10].Controls[1]);


                if (!(((CollectionTransaction)(e.Row.DataItem)).Status.ToLower().Equals("cancelled")))
                {
                    chqAmount = chqAmount + (double)((CollectionTransaction)(e.Row.DataItem)).CollectionAmount;
                }
                //jayanga
                if (e.Row.Cells[5].Text.Equals("LOAN",StringComparison.OrdinalIgnoreCase))
                {
                    txtLoanInt.Text = ((CollectionTransaction)(e.Row.DataItem)).LoanInterest.ToString();
                    if (HQFMgr || Cachier)
                    {
                        txtLoanInt.ReadOnly = false;
                    }
                    else
                    {
                        txtLoanInt.ReadOnly = true;
                    }
                    
                }
                else
                {
                    txtLoanInt.Text = ((CollectionTransaction)(e.Row.DataItem)).LoanInterest.ToString();
                    txtLoanInt.ReadOnly = true;
                }

                if (e.Row.Cells[5].Text.Equals("RENEWAL", StringComparison.OrdinalIgnoreCase) || e.Row.Cells[5].Text.Equals("LOAN", StringComparison.OrdinalIgnoreCase))
                {

                    txtContract.Text = ((CollectionTransaction)(e.Row.DataItem)).ContractNo;
                    txtContract.ReadOnly = true;
                }
                else if (e.Row.Cells[5].Text.Equals("DEPOSIT",StringComparison.OrdinalIgnoreCase))
                {
                    if (HQFMgr || Cachier)
                    {
                        txtContract.ReadOnly = false;
                    }
                    else
                    {
                        txtContract.ReadOnly = true;
                    }
                    
                    txtContract.Text = ((CollectionTransaction)(e.Row.DataItem)).ClientID;
                }

                //Set Cheqe Details
                TextBox txtChqDetails = (TextBox)(e.Row.Cells[6].Controls[1]);
                txtChqDetails.Text = ((CollectionTransaction)(e.Row.DataItem)).CheqNo + "-" + ((CollectionTransaction)(e.Row.DataItem)).BankCode + "-" + ((CollectionTransaction)(e.Row.DataItem)).BranchCode;
                if (HQFMgr || Cachier)
                {
                    txtChqDetails.ReadOnly = false;
                }
                else
                {
                    txtChqDetails.ReadOnly = true;
                }



                lblChqAmt.Text = chqAmount.ToString();
                //
                //Set Bank Status
                if (((CollectionTransaction)(e.Row.DataItem)).BankedStatus == "No")
                {
                    ddlBankStatus.SelectedIndex = 1;
                }
                else
                {
                    ddlBankStatus.SelectedIndex = 0;
                }

                //Sete Navigate URL and Texts to HyperLinks
                lnkRecNo.Text = ((CollectionTransaction)(e.Row.DataItem)).ReceiptNo.ToString();
                
                //set Javascript String
                //lnkRecNo.Attributes.Add("OnClientClick", SetJavascriptString(lnkRecNo.Text));

                hypDetails.NavigateUrl = "~/Collection/TransactionRemarkDetails.aspx?RecNo=" + ((CollectionTransaction)(e.Row.DataItem)).ReceiptNo.ToString() + "&ContNo=" + txtContract.Text + "&SOCode=" + SOCode + "&CSSNo=" + CSSNo;

                if (e.Row.Cells[7].Text.Equals("VERIFIED", StringComparison.OrdinalIgnoreCase))
                {
                    if (ViewState["UserType"] != null && Convert.ToInt32(ViewState["UserType"]) != 1)
                    {
                        ((CheckBox)(e.Row.Cells[0].Controls[1])).Enabled = false;
                    }

                    if (HQFMgr)
                    {
                        ((CheckBox)(e.Row.Cells[0].Controls[1])).Enabled = true;
                    }
                    txtContract.ReadOnly = true;
                    txtLoanInt.ReadOnly = true;
                    txtChqDetails.ReadOnly = true;
                    ddlBankStatus.Enabled = false;
                }

                cheqDetails[Convert.ToInt64(lnkRecNo.Text)] = txtChqDetails.Text.Trim();
            }
            
        }

        private string SetJavascriptString(string receptNo)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<script language='javascript' type='text/javascript'>");
            sb.AppendLine("function PopReceipt(){");
            sb.AppendLine("window.open(");
            sb.AppendLine("'../Collection/ReceiptDatails.aspx?RecNo="+receptNo+"&SOCode="+SOCode+"',");
            sb.AppendLine("'recPopup',");
            sb.AppendLine("'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=yes,width=400,height=450,left=100, top=100,screenX=100,screenY=100'");
            sb.AppendLine(")");
            sb.AppendLine("}");
            sb.AppendLine("</script>");


            Page.ClientScript.RegisterClientScriptBlock(GetType(), "popup"+receptNo, sb.ToString());
            return sb.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int rowNum = 0;
            int chekedCount = 0;
            foreach (GridViewRow gvr in GridVCash.Rows)
            {
                if (gvr.RowType == DataControlRowType.DataRow)
                {
                    if (((CheckBox)(gvr.Cells[0].Controls[1])).Checked)
                    {
                        chekedCount++;
                        int res = finFac.VerifyCollection(SOCode,
                                                        Convert.ToInt64(((LinkButton)gvr.Cells[1].Controls[1]).Text.Trim()),
                           
                                                        "VERIFIED",User.Identity.Name);
                        if (res == 1)
                        {
                            rowNum++;
                        }
                    }
                }
            }

            foreach (GridViewRow gvr in GridVCheque.Rows)
            {
                if (gvr.RowType == DataControlRowType.DataRow)
                {
                    if (((CheckBox)(gvr.Cells[0].Controls[1])).Checked)
                    {
                        chekedCount++;
                        int res = finFac.VerifyCollection(SOCode,
                                                        Convert.ToInt64(((LinkButton)gvr.Cells[1].Controls[1]).Text.Trim()),
                                                        "VERIFIED", User.Identity.Name);
                        if (res == 1)
                        {
                            rowNum++;
                        }
                    }
                }
            }
            if (chekedCount > 0)
            {
                if (rowNum > 0)
                {
                    MassageSummery("IIS405");
                }
            }
            else
            {
                MassageSummery("IIS533");
            }
            showGridData();
        }

        protected bool verifyGridData()
        {
            bool flag = true;

            foreach (GridViewRow gvr in GridVCash.Rows)
            {
                if (gvr.RowType == DataControlRowType.DataRow)
                {
                    if (((CheckBox)(gvr.Cells[0].Controls[1])).Checked && !isValidClientId(((TextBox)gvr.Cells[2].Controls[1]).Text))
                    {
                        flag = false;
                        break;
                    }
                }
            }

            if (flag)
            {
                foreach (GridViewRow gvr in GridVCheque.Rows)
                {
                    if (gvr.RowType == DataControlRowType.DataRow)
                    {
                        if (((CheckBox)(gvr.Cells[0].Controls[1])).Checked && !isValidClientId(((TextBox)gvr.Cells[2].Controls[1]).Text))
                        {
                            flag = false;
                            break;
                        }
                    }
                }
            }
            return flag;
        }

        private bool isValidClientId(string ClientId)
        {
            return (ClientId.Length == 8) ? true : false;
        }

        protected void btnVerify_Click(object sender, EventArgs e)
        {
            Dictionary<long, string> cheqDetails = new Dictionary<long, string>();
            cheqDetails = (Dictionary<long, string>)Session["CheqDetailsDic"];
            string validCash = ValidateGridData(GridVCash);
            string validCheq = ValidateGridData(GridVCheque);
            int chekedCount = 0;

            if (validCash == "VALID" && validCheq == "VALID")
            {
                int rowCount = 0;
                if (verifyGridData())
                {
                    foreach (GridViewRow gvr in GridVCash.Rows)
                    {
                        if (gvr.RowType == DataControlRowType.DataRow)
                        {
                            if (((CheckBox)(gvr.Cells[0].Controls[1])).Checked)
                            {
                                chekedCount++;
                                int res = finFac.UpdateCollectionGrid(SOCode,
                                                                Convert.ToInt64(((LinkButton)gvr.Cells[1].Controls[1]).Text.Trim()),
                                                                ((TextBox)gvr.Cells[2].Controls[1]).Text,
                                                                ((TextBox)gvr.Cells[3].Controls[1]).Text,
                                                                gvr.Cells[5].Text,
                                                                ((DropDownList)gvr.Cells[9].Controls[1]).SelectedItem.Text, "", "", "", User.Identity.Name);
                                //Verify
                                //res = finFac.VerifyCollection(SOCode,
                                //                            Convert.ToInt64(((LinkButton)gvr.Cells[1].Controls[1]).Text.Trim()),
                                //                            "VERIFIED", User.Identity.Name);

                                if (res == 1)
                                {
                                    rowCount++;
                                }
                            }

                        }
                    }

                    foreach (GridViewRow gvr in GridVCheque.Rows)
                    {
                        if (gvr.RowType == DataControlRowType.DataRow)
                        {
                            LinkButton lnkRecNo = (LinkButton)(gvr.Cells[1].Controls[1]);
                            TextBox txtCheqDetails = (TextBox)(gvr.Cells[6].Controls[1]);
                            string[] bankDetails = new string[3];
                            bankDetails = ((TextBox)(gvr.Cells[6].Controls[1])).Text.Trim().Split('-');

                            if (((CheckBox)(gvr.Cells[0].Controls[1])).Checked)
                            {
                                chekedCount++;
                                int res;

                                //Update change log
                                if (cheqDetails[Convert.ToInt64(lnkRecNo.Text)].ToString() != txtCheqDetails.Text.Trim())
                                {
                                    res = finFac.UpdateChangeLog(SOCode, Convert.ToInt64(lnkRecNo.Text), txtCheqDetails.Text, User.Identity.Name);
                                }


                                res = finFac.UpdateCollectionGrid(SOCode,
                                                                Convert.ToInt64(((LinkButton)gvr.Cells[1].Controls[1]).Text.Trim()),
                                                                ((TextBox)gvr.Cells[2].Controls[1]).Text,
                                                                ((TextBox)gvr.Cells[3].Controls[1]).Text,
                                                                gvr.Cells[5].Text,
                                                                ((DropDownList)gvr.Cells[10].Controls[1]).SelectedItem.Text,
                                                                bankDetails[0], bankDetails[1], bankDetails[2], User.Identity.Name);
                                //Verify
                                //res = finFac.VerifyCollection(SOCode,
                                //                            Convert.ToInt64(((LinkButton)gvr.Cells[1].Controls[1]).Text.Trim()),
                                //                            "VERIFIED", User.Identity.Name);


                                if (res == 1)
                                {
                                    rowCount++;
                                }
                            }
                        }
                    }

                    if (chekedCount > 0)
                    {
                        if (rowCount > 0)
                        {
                            MassageSummery("IIS405");
                            showGridData();
                        }
                    }
                    else
                    {
                        MassageSummery("IIS533");
                    }
                }
                else
                {
                    MassageSummery("IIS501");
                }
                //showGridData();
            }

            else if (validCash == "ZEROLOAN" || validCheq == "ZEROLOAN")
            {
                MassageSummery("IIS542");
            }

            else if (validCash == "STRLOAN" || validCheq == "STRLOAN")
            {
                MassageSummery("IIS543");
            }

            else if (validCash == "INVALID" || validCheq == "INVALID")
            {
                MassageSummery("IIS544");
            }
            else if (validCheq == "INVALIDCHQ")
            {
                MassageSummery("IIS539");
            }
            
        }        

        private string ValidateGridData(GridView grid)
        {
            foreach (GridViewRow gvr in grid.Rows)
            {
                //TextBox txtContract = (TextBox)(gvr.Cells[2].Controls[1]);
                //TextBox txtLoanInt = (TextBox)(gvr.Cells[3].Controls[1]);

                if (gvr.RowType == DataControlRowType.DataRow && ((CheckBox)(gvr.Cells[0].Controls[1])).Checked)
                {
                    if (gvr.Cells[5].Text.Equals("LOAN",StringComparison.OrdinalIgnoreCase))
                    {
                        //txtLoanInt.Text = ((CollectionTransaction)(gvr.DataItem)).LoanInterest.ToString();
                        TextBox txtLoanInt = (TextBox)(gvr.Cells[3].Controls[1]);
                        try
                        {
                            if (Convert.ToDouble(txtLoanInt.Text) < 0)
                            {
                                return "ZEROLOAN";
                            }
                        }
                        catch(FormatException)
                        {
                            return "STRLOAN";
                        }

                        if (Convert.ToDouble(txtLoanInt.Text) > Convert.ToDouble(gvr.Cells[4].Text))
                            return "STRLOAN";
                    }
                    

                    if (gvr.Cells[5].Text.Equals("DEPOSIT",StringComparison.OrdinalIgnoreCase))
                    {
                        TextBox txtContract = (TextBox)(gvr.Cells[2].Controls[1]);
                        if (txtContract.Text.Trim() == "" || txtContract.Text.Equals("New",StringComparison.OrdinalIgnoreCase))
                        {
                            return "INVALID";
                        }
                    }

                    if (grid.ID == "GridVCheque" && ((TextBox)(gvr.Cells[6].Controls[1])).Text.Trim().Split('-').Length != 3)
                    {
                        return "INVALIDCHQ";
                    }
                }
            }
            return "VALID";
        }

        #region IDataPage Members

        void IDataPage.Create()
        {
            throw new NotImplementedException();
        }

        void IDataPage.View()
        {
            throw new NotImplementedException();
        }

        void IDataPage.Update()
        {
            throw new NotImplementedException();
        }

        void IDataPage.Delete()
        {
            throw new NotImplementedException();
        }

        void IDataPage.Cancel()
        {
            throw new NotImplementedException();
        }

        void IDataPage.GetId(GridViewRow row)
        {
            throw new NotImplementedException();
        }

        #endregion

        protected void GridVCash_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow &&
                (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
            {
                CheckBox chkBxSelect = (CheckBox)e.Row.Cells[0].FindControl("CheckBox1");
                CheckBox chkBxHeader = (CheckBox)this.GridVCash.HeaderRow.FindControl("chkBxHeader");
                chkBxSelect.Attributes["onclick"] = String.Format(
                    "javascript:ChildClick(this,'{0}');", chkBxHeader.ClientID
                    );
            }
        }

        protected void GridVCheque_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow &&
                (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
            {
                CheckBox chkBxSelect = (CheckBox)e.Row.Cells[0].FindControl("CheckBox1");
                CheckBox chkBxHeader = (CheckBox)this.GridVCheque.HeaderRow.FindControl("chkBxHeader");
                chkBxSelect.Attributes["onclick"] = String.Format(
                    "javascript:ChildClick1(this,'{0}');", chkBxHeader.ClientID
                    );
            }
        }
    }
}
